<template>
  <div class="mobile-box take-box">
    <div class="tabs-box">
      <div class="top">{{ pageTitle }}</div>
      <div class="top_b">订单编号：{{ orderNum }}</div>
    </div>

    <div class="list-box">
      <ul
        class="infinite-list"
        v-infinite-scroll="loadMore"
        style="overflow: auto"
        infinite-scroll-disabled="busy"
      >
        <li
          class="item infinite-list-item"
          v-for="(box, index) in takeOrderBoxList"
          :key="index"
        >
          <TakeOrderBox
            :box="box"
            @examine="examine"
          />
        </li>
      </ul>

      <div class="no-data" v-if="takeOrderBoxList.length === 0">
        <img src="@/assets/images/mobile/nothing.png" alt="暂无记录" />
        <span>暂无记录！</span>
      </div>

      <div class="user-bottom">
        <div class="user">
          {{ userPhone }}
        </div>
        <div class="ge">|</div>
        <div class="out" @click="goOut">退出</div>
      </div>
    </div>

    <div class="fixed-bottom" v-if="takeOrderBoxList.length > 0">
      <div class="approval-all" @click="approvalAll">全部通过</div>
    </div>

    <el-backtop target=".list-box" :bottom="50" :right="10">
      <i class="el-icon-caret-top"></i>
    </el-backtop>

    <SpecialDialog
      :dialogWidth="'300px'"
      :title="dialogTitle"
      :visible="dialogVisible"
      @close="closeDialog"
      @confirm="examineSubmit"
      :showReason="pictStatus === 3"
    />
  </div>
</template>

<script>
import {
  getTakeOrder,
  examineApplyOrderBox,
} from "@/api/access-manage/index";
import { userLogout } from "@/api/member/index";

import TakeOrderBox from "./components/TakeOrderBox";
import SpecialDialog from "./components/SpecialDialog";
export default {
  name: "approvalTakeOrder",
  components: { TakeOrderBox, SpecialDialog },
  computed: {},
  data() {
    return {
      orderNum: "",
      pageTitle: "",
      userPhone: "",

      takeOrderBoxList: [],
      pageSize: 10,
      pageNum: 1,
      total: 0,

      dialogTitle: "",
      dialogVisible: false,

      pictStatus: "",
      orderBoxId: "",
      cancelReason: "",

      busy: false,
    };
  },
  created() {
    this.orderNum = this.$route.params.orderNum;
    this.getTakeOrderList();
  },
  mounted() {
    this.pageTitle = this.$route.meta.title;
    this.userPhone = JSON.parse(localStorage.getItem("localUserInfo")).mobile;
  },
  methods: {
    //   加载更多
    loadMore() {
      this.busy = true;
      if (this.total > 0 && this.total === this.takeOrderBoxList.length) return;
      this.pageNum = this.pageNum + 1;
      this.getMoreList();
    },
    //  待审批 列表 - 加载更多
    getMoreList() {
      let params = {
        picStatus: 1,
        orderNum: this.orderNum,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      new Promise((resolve) => {
        getTakeOrder(params)
          .then((res) => {
            res.data && res.data.map((e) => {
              e["fileList"] = [];
              e.userBoxFileList && e.userBoxFileList.map((m) => {
                e.fileList.push(m.path);
              });
            });
            this.takeOrderBoxList = this.takeOrderBoxList.concat(res.data);

            this.total = res.total;
            setTimeout(() => {
              this.busy = false;
            }, 1200);
          })
          .catch((err) => {
            this.busy = false;
          });
      });
    },
    // 获得 档案提走_审批 列表
    getTakeOrderList() {
      let params = {
        picStatus: 1,
        orderNum: this.orderNum,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: "加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.5)",
      });

      new Promise((resolve) => {
        getTakeOrder(params)
          .then((res) => {
            res.data && res.data.map((e) => {
              e["fileList"] = [];
              e.userBoxFileList && e.userBoxFileList.map((m) => {
                e.fileList.push(m.path);
              });
            });
            this.takeOrderBoxList = res.data;

            this.total = res.total;
            setTimeout(() => {
              loading.close();
            }, 1200);
          })
          .catch((err) => {
            loading.close();
          });
      });
    },
    // 重新加载列表
    getListAgain() {
      this.busy = false;
      this.pageSize = 10;
      this.pageNum = 1;

      this.getTakeOrderList();
    },

    // 全部通过
    approvalAll() {
      this.dialogVisible = true;
      // pictStatus 无id
      this.pictStatus = 2;
      this.orderBoxId = null;
      this.dialogTitle = "确定通过全部申请吗？";
    },
    // 审核
    examine(data) {
      // pictStatus 2已通过 3未通过
      this.pictStatus = data.examineType;
      this.orderBoxId = data.id;
      this.cancelReason = data.cancelReason;
      if (data.examineType === 3) {
        this.dialogTitle = "确定拒绝该申请吗？";
      } else if (data.examineType === 2) {
        this.dialogTitle = "确定通过该申请吗？";
      }
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 弹框确定 - 通过 / 拒绝该申请
    examineSubmit(data) {
      this.dialogVisible = false;
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: "加载中",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.5)",
      });
      const queryData = {
        pictStatus: this.pictStatus,
        orderBoxId: this.orderBoxId,
        orderNum: this.orderNum,
        reason: this.pictStatus === 3 ? data.cancelReason : null,
      };

      // 调用接口 审核
      examineApplyOrderBox(queryData)
        .then((res) => {
          this.$message({
            type: "success",
            message:
              this.pictStatus === 2 ? "档案提走窍已通过" : "档案提走窍已拒绝",
          });
          loading.close();
          this.getListAgain();
        })
        .catch((err) => {
          loading.close();
          this.getListAgain();
        });
    },
    // 退出
    goOut() {
      userLogout().then((res) => {
        this.$store.commit("app/REMOVE_USERINFO");
        this.$store.commit("app/CLEAR_LOCAL_USERINFO");
        localStorage.clear();

        this.$router.push({ path: "/mobileLogin" });
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.mobile-box {
  width: 100%;
  height: 100%;
  position: relative;
  .top {
    height: 50px;
    width: 100%;
    background-color: #f88123;
    line-height: 50px;
    color: #fff;
    text-align: center;
    font-size: 16px;
  }
  .top_b {
    height: 30px;
    width: 100%;
    line-height: 30px;
    padding-left: 10px;
  }
  .tabs-box {
    position: absolute;
    top: 0;
    width: 100%;
    background-color: #fff;
    z-index: 100;
  }

  .list-box {
    background-color: #f5f5f5;
    padding: 100px 0.1rem 50px;
    height: 100vh;
    overflow-x: hidden;
    .item {
      margin-bottom: 20px;
      padding: 0 10px;
    }

    .no-data {
      height: calc(100% - 50px);
      margin: 0 10px 20px;

      display: flex;
      flex-direction: column;
      // align-items: center;
      text-align: center;
      img {
        width: 100px;
        margin: 50px auto 20px;
      }
    }

    .user-bottom {
      display: flex;
      padding: 0 10px;
      margin-bottom: 10px;
      .ge {
        margin: 0 6px;
      }
      .out {
        color: #f88123;
      }
    }
  }
  .fixed-bottom {
    width: 100%;
    position: absolute;
    bottom: 0;
    background-color: #fff;
    .approval-all {
      line-height: 40px;
      background-color: #f88123;
      color: #fff;
      text-align: center;
      cursor: pointer;
    }
  }

  // 修改tab样式
  ::v-deep  .el-tabs__header {
    margin: 0;
    .el-tabs__nav {
      width: 100%;
      .el-tabs__item {
        width: 33.3%;
        text-align: center;
      }
    }
  }
}

@media only screen and (min-width: 769px) {
  .take-box {
    max-width: 500px;
    margin: 0 auto;
  }
}
</style>
